//顺序栈实现

#include<stdio.h>
#include<malloc.h>
#define MAXSIZE 500
#define ADDISZE 100

typedef struct 
{
    int *bottom;
    int *top;
    int stacksize;     //当前已分配空间
}SqStack;

int main()
{
    SqStack S;
    S.bottom=(int *)malloc((MAXSIZE)*sizeof(int));
    S.top=S.bottom;
    S.stacksize=MAXSIZE;
    int Push(SqStack *S,int e);
    int Pop(SqStack *S);
    int GetTop(SqStack *S);
    int type,e;
    printf("---------------功能菜单---------------\n");
    printf("【1】入栈   【2】出栈   【3】取栈顶元素\n");
    printf("--------------------------------------\n");
    printf("请输入你想要的功能：");
    scanf("%d",&type);
    while(type!=-1)
    {
        switch(type)
        {
            case 1:
            {
                printf("请输入需要入栈的元素：");
                scanf("%d",&e);
                Push(&S,e);
                printf("请输入你想要的功能：");
                break;
            }
            case 2:
            {
                e=Pop(&S);
                printf("元素出栈成功：%d\n",e);
                printf("请输入你想要的功能：");
                break;
            }
            case 3:
            {
                e=GetTop(&S);
                printf("栈顶元素的值：%d\n",e);
                printf("请输入你想要的功能：");
                break;
            }
        }
        scanf("%d",&type);
    }
}

int Push(SqStack *S,int e)
{
    if(S->top-S->bottom>=S->stacksize)
    {
        S->bottom=(int *)realloc(S->bottom,(ADDISZE+MAXSIZE)*sizeof(int));
        S->stacksize+=ADDISZE;
    }
    *(S->top)=e;
    S->top++;
}

int Pop(SqStack *S)
{
    int e;
    S->top--;
    e=*(S->top);
    return(e);
}

int GetTop(SqStack *S)
{
    int e;
    e=*(S->top-1);
    return(e);
}